package com.wf.demo.info.web;


import com.wf.demo.info.pojo.Device;
import com.wf.demo.info.pojo.Parse;
import com.wf.demo.info.service.ParseService;
import com.wf.demo.info.util.Page4Navigator;
import com.wf.demo.info.util.Result;
import com.wf.demo.sys.annotation.SysLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class ParseController {
    @Autowired
    ParseService parseService;
    @GetMapping("/models/{mid}/parses")
    @RequiresPermissions("info:parse")
    public  List<Parse> list(@PathVariable("mid")int mid){
            return  parseService.list(mid);
    }
    @GetMapping("/parses/{id}")
    public Object get(@PathVariable("id")int id){
        Parse bean= parseService.get(id);
        return bean;
    }

    @SysLog("保存解析信息")
    @PostMapping("/parses")
    @RequiresPermissions("info:parse")
    public Object add(@RequestBody Parse bean) throws Exception{
        String name=bean.getDataName();
        int id=bean.getId();
        boolean exit=parseService.isExist(name,id);
        if(exit){
            String message="此解析名已存在，添加失败";
            return Result.addFail(message);
        }
        parseService.add(bean);
        return Result.addSuccess(bean);
    }
    @SysLog("删除解析信息")
    @DeleteMapping("/parses/{id}")
    @RequiresPermissions("info:parse")
    public String delete(@PathVariable("id")int id){
        parseService.delete(id);
        return null;
    }

    @SysLog("修改解析信息")
    @PutMapping("/parses")
    @RequiresPermissions("info:parse")
    public Object update(@RequestBody Parse bean) throws Exception{

        boolean exit=parseService.isExist(bean);
        if(exit){
            String message="此解析名已存在，添加失败";
            return Result.addFail(message);
        }
        parseService.update(bean);
        return Result.addSuccess(bean);
    }
}
